package com.weibo4terios.ui;

import java.util.HashMap;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.weibo.net.WeiboParameters;
import com.weibo4terios.R;
import com.weibo4terios.conts.CommonStrs;
import com.weibo4terios.logic.MainService;
import com.weibo4terios.logic.OAuth2Activity;
import com.weibo4terios.logic.Task;
import com.weibo4terios.ui.util.IWeiboActivity;
import com.weibo4terios.util.SaveParam;
import com.weibo4terios.util.WeiboUtil;

/**
 * LoginActivity.java 2012-10-29 下午6:22:50
 * 
 * @author Wang Tengrui
 */
public class LoginActivity extends Activity implements IWeiboActivity {

	public static final int REFRESH_LOGIN = 1;// 登陆
	private String token;

	private WeiboParameters nUserParame;
	private Button loginBtn;// 登录按钮
	private Button authBtn;// 授权按钮
	private TextView loginUserTv;

	private ProgressDialog progressDialog;

	@SuppressWarnings("static-access")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(getWindow().FEATURE_NO_TITLE);// 取消标题显示
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		MainService.allActivity.add(this);
		
		
		loginBtn = (Button) findViewById(R.id.loginBtn);
		authBtn = (Button) findViewById(R.id.authBtn);
		loginUserTv = (TextView) findViewById(R.id.tv_login_user);
		
		init();
		
		
		
		
		loginBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (null == nUserParame.getValue("uid")) {
					Toast.makeText(LoginActivity.this, "首次使用，请点授权按钮", Toast.LENGTH_SHORT).show();
				} else {
					if (Long.parseLong(nUserParame.getValue("expires_date")) > System.currentTimeMillis()) {
						goHome();// 跳转到主界面
					} else {
						Toast.makeText(LoginActivity.this, "授权已经过期，请点击授权按钮重新授权", Toast.LENGTH_SHORT).show();
					}
				}
			}
		});

		authBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				goOAuth(LoginActivity.this);
			}
		});
		
		

	}

	@Override
	protected void onResume() {
		super.onResume();
		// 隐式意图启动MainService
		Intent intent = new Intent("com.weibo4terios.logic.MainService");
		this.startService(intent);
	}

	@Override
	public void init() {
		initView();
		if (WeiboUtil.checkNet(LoginActivity.this)) {
		} else {
			MainService.alertNetError(this);
		}

	}

	private void initView() {
		nUserParame = SaveParam.getNowUser(this);
		if (nUserParame.getValue("uid") != null) {
			TextView tishiTextView = (TextView)findViewById(R.id.tishi);
			tishiTextView.setText("");
			token = nUserParame.getValue("access_token");
			loginUserTv.setText("王腾瑞");
		}else {
			TextView tishiTextView = (TextView)findViewById(R.id.tishi);
			tishiTextView.setText("首次使用本应用，请点击授权按钮");
		}
	}
	
	@SuppressWarnings("unchecked")
	public void goHome() {
		if (null == progressDialog) {
			progressDialog = new ProgressDialog(LoginActivity.this);
		}
		progressDialog.setMessage("正在登录中。。。");
		progressDialog.show();
		@SuppressWarnings("rawtypes")
		HashMap param = new HashMap();
		param.put("token", token);
		param.put("uid", nUserParame.getValue("uid"));

		Task loginTask = new Task(CommonStrs.TASK_USER_LOGIN, param);
		MainService.addTask(loginTask);
	}

	public void goOAuth(Context context) {
		context.startActivity(new Intent(context, OAuth2Activity.class));

	}

	@Override
	public void refresh(Object... param) {
		int flag = ((Integer) param[0]).intValue();// 获取第一个参数
		switch (flag) {
		case REFRESH_LOGIN:
			Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
			Log.i("LoginActivity", "refresh()：" + "" + ((Integer) param[0]).intValue());
			 if (progressDialog != null) {
			 progressDialog.dismiss();
			 }
			this.startActivity(new Intent(this, HomeActivity.class));
			MainService.allActivity.remove(this);
			finish();
			break;

		}
	}

}
